!<template>
  <el-dialog :visible.sync="myShow" title="添加产品数据" width="600px">
    <el-row>
      <el-col :span="12">
        <el-form ref="form" :model="form" label-width="100px">
          <el-form-item
            label="日期"
            prop="date"
            :rules="[{ required: true, message: '日期不能为空' }]"
          >
            <el-date-picker
              value-format="yyyy-MM-dd"
              v-model="form.date"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item
            label="数值："
            prop="value"
            style="width:300px"
            :rules="[{ required: true, message: '数值不能为空' }]"
          >
            <el-input
              type="number"
              v-model="form.value"
              placeholder="请输入数值"
            ></el-input>
          </el-form-item>
        </el-form>
        <div class="dialog-footer text-center">
          <el-button type="primary" @click="onSubmit">确 定</el-button>
        </div>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { Message } from "element-ui";

export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ""
    },
    info: {
      type: Object,
      default: {}
    },
    dataList: {
      type: Array,
      default: new Array()
    }
  },
  data() {
    return {
      form: { date: "", value: "" },
      myShow: false,
      curObj: {}
    };
  },
  components: {},
  methods: {
    onSubmit() {
      const { validate } = this.$refs["form"];
      validate(valid => {
        if (valid) {
          let myDate = this.form.date,
            isEdit = this.form.isEdit;
          //编辑
          if (isEdit) {
            let _date = this.curObj.date;
            if (
              this.dataList
                .filter(item => item.date !== _date)
                .some(item => item.date === myDate)
            ) {
              Message({
                message: "不能重复添加已有日期数据！",
                type: "error",
                duration: 2 * 1000
              });
              return;
            }
          } else {
            if (this.dataList.some(item => item.date === myDate)) {
              Message({
                message: "不能重复添加已有日期数据！",
                type: "error",
                duration: 2 * 1000
              });
              return;
            }
          }
          this.$emit("getSingleInfo", { ...this.form });
          this.myShow = false;
        }
      });
    }
  },
  watch: {
    myShow(newV, oldV) {
      if (newV !== oldV) {
        this.$emit("on-toggle", newV);
      }
    },
    show(newV, oldV) {
      this.myShow = newV;
    },
    info(newV, oldV) {
      this.form = this.$_.cloneDeep(newV);
      this.curObj = this.$_.cloneDeep(newV);
    }
  }
};
</script>
<style lang="scss" scoped></style>
